/**
* @class
* A bizproc action trigger driven by a click event bound to the given dom element.
* This has the flexibility to be an anchor or a div that is turned into a jQuery UI button widget.
* This class maintains the css class state of the attached DOM element based on the state
* of the bizproc action.  This allows css to be applied to hide the button when the action is totally unavailable
* to the user or just disable it if the action is just currently not available.
<p>The main DOM element this is attached to will have the following css classes added/removed from it
based on the action state:</p>
<ul>
	<li>ksg-enabled</li>
	<li>cs-disabled - would typically cause the element to be grayed out in some way</li>
	<li>ksg-notAllowed - would typically cause the element to be hidden</li>
</ul>
<p>It is recommended that the DOM element be hidden by default so that the element isnt visible
until the jQuery widget has attached to it and been able to setup the class name.  Use ksg-enabled
to make the element visible.  This will keep buttons that should be hidden from being visisble as the page
is loading and then get hidden after the ksg-notAllowed class is added.</p>
<pre>&lt;div class="ksg-actionButton" data-actions='items.updateBizObj'&gt;Update&lt;/div&gt;</pre>
<p>This jQuery widget registers with the central widget factory using <b>ksg-actionButton</b>.</p>
<p>use the <b>data-actions</b> attribute to specify a comma delimited list of bizproc action names, which are formatted
as [bizProcId].[actionName].</p>
<p>This widget can be attached to:<p>
<ul>
	<li>&lt;a&gt; - Use an anchor.  If user disables javascript anchor href could actually be used as a fallback.</li>
	<li>&lt;div&gt; - Use a div, span, or other basic widget.</li>
</ul>
<p>You can also manually create the widget with javascript code similar to the following:</p>
<pre>
$("#myDisplayId").actionButton({
	actions: "items.updateBizObj"
});
</pre>
*
*
* @see ksg.ui.Action
* @see ksg.ui.BizProc
* @meta-type jQuery widget
* @Depends ksg.baseWidget.js
*/
ksg.ui.widgets.actionButton = {
	options: {
	},
	
	_create: function() { var self = this; var $this = this.element; var options = this.options;
		ksg.App.bizProcMgr.registerWidget(this);
		$this.bind('click', function(event) {
			var switchName = $this.data('switch');
			if(switchName) {
				var session = ksg.App.SessionData();
				var sw = session[switchName];
				session[switchName] = sw?false:true;
			}
			
			var actionStr = $this.data('actions'), actions = actionStr.split(/\s+/);
			ksg.App.bizProcMgr.actionTriggered(actionStr, function() {
				$.each(actions, function(){
					var actionParts = this.split('.');
					var bizProc = ksg.App.bizProcMgr.getBizProc(actionParts[0]);
					if(bizProc) {
						var action = bizProc.getAction(actionParts[1]);
						if(action && action.availability == ksg.ui.Availability.Enabled) {
							action.trigger();						
						}
						else {
							//alert("action disabled");
						}
					}
				});			
			});
		});
	},
	
	_firstBizProc: function() {
		var $this = this.element;
		var actions = $this.data('actions').split(/\s+/);
		var actionParts = actions[0].split('.');
		return ksg.App.bizProcMgr.getBizProc(actionParts[0]);
	},
	
	_getActionAvailability: function() {
		var $this = this.element;
		var availability = ksg.ui.Availability.None;
		
		var actions = $this.data('actions').split(/\s+/);
		var actionParts = actions[0].split('.');
		var bizProc = ksg.App.bizProcMgr.getBizProc(actionParts[0]);
		if(bizProc) {
			var action = bizProc.getAction(actionParts[1]);
			if(action)
				availability = action.availability;
		}
		
		return availability;
	},
	
	destroy: function() {
		ksg.App.bizProcMgr.unregisterWidget(this);
	},
	
	//--- The following are required IWidget interface methods
	getParentBizProcId: function() {
		var bizProc = this._firstBizProc();
		if(bizProc)
			return bizProc.id;
		return null;
	},	
	getWidgetType: function() {return ksg.ui.BizProcWidgetType.ActionTrigger;},
	
	//--- The following are required IActionTrigger interface methods
	getActions: function() {
		return this.options.actions;
	},
	
	setupStateFromActions: function() {
		var $this = this.element;
		var availability = this._getActionAvailability();
		
		if(availability == ksg.ui.Availability.Disabled) {
			$this.removeClass("ksg-notAllowed ksg-enabled").addClass("ksg-disabled");
			if($this.hasClass("ui-button"))
				$this.button("disable");
		}
		else if(availability == ksg.ui.Availability.Enabled) {
			$this.removeClass("ksg-notAllowed ksg-disabled").addClass("ksg-enabled");
			if($this.hasClass("ui-button"))
				$this.button("enable");
		}
		else
			$this.removeClass("ksg-disabled ksg-enabled").addClass("ksg-notAllowed");
	}
};

$.widget("ksg.actionButton", ksg.ui.widgets.actionButton);
ksg.ui.widgetMap["ksg-actionButton"] = function ($el, options) {
	$el.actionButton(options); 
};
